<?php

namespace Member\Controller;

/**
 * ProductPromotion 控制器类
 *
 * @since 1.0 <2016-8-23> SoChishun <14507247@qq.com> Added.
 */
class ProductPromotion extends AppbaseController {

    function indexSearch() {
        $search = I('param.');
        $where = array();
        if (!empty($search['word'])) {
            $where['title|code'] = array('like', '%' . $search['word'] . '%');
        }
        // $where['site_id'] = $this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    function index() {
        $asearch = $this->indexSearch();
        $m_promotion = new \XWAM\Model\ProductPromotionModel();
        $list = $m_promotion->selectPaging($page, array('where' => $asearch['where'], 'order' => 'sort,id'), array('page_params' => $asearch['search']));
        $this->assign('list', $list);
        $this->assign('page', $page->showBootstrap());
        $this->assign('search', $asearch['search']);
        $this->display();
    }

    function edit($id = 0, $category_id = 0) {
        $data = array();
        if ($id) {
            $m_promotion = new \XWAM\Model\ProductPromotionModel();
            $data = $m_promotion->find($id);
            unset($m_promotion);
        }
        if (!$data) {
            $data = array('user_name' => $this->userName, 'site_id' => $this->siteId);
        }
        if ($category_id) {
            $data['category_id'] = $category_id;
        }
        $this->assign('meta_title', $id ? '编辑' . $data['title'] : '新增促销活动');
        $this->assign('data', $data);
        $this->assign('content_codes', $this->content_codes);
        $this->display();
    }

    function editSave() {
        $m_promotion = new \XWAM\Model\ProductPromotionModel();
        $result = $m_promotion->savePromotion();
        $this->dialogJump($result['status'], $result['info']);
    }

    function product_goods_list($promid = 0) {
        if (!$promid) {
            $this->error('参数无效!');
        }
        $where = array();
        $search = I('param.');
        $word = I('word');
        if ($word) {
            $where['title'] = array('like', "%$search%");
        }
        $m_promotion = new \XWAM\Model\ProductPromotionModel();
        $pid = $m_promotion->where(array('id' => $promid))->getField('product_ids');
        unset($m_promotion);
        if (!$pid) {
            $this->dialogClose(false, array('error' => '无商品!'));
        }
        $where['id'] = array('in', $pid);
        $m_goods = new \XWAM\Model\ProductGoodsModel();
        $list = $m_goods->selectPaging($page, array('where' => $where, 'order' => 'id desc'), array('page_params' => $search));
        unset($m_goods);
        $m_category = new \XWAM\Model\ProductCategoryModel();
        $categories = $m_category->getIdTextList($this->siteId, true);
        unset($m_category);
        $aout = array();
        if ($list && $categories) {
            foreach ($list as &$row) {
                $cid = $row['category_id'];
                $row['category_text'] = isset($categories[$cid]) ? $categories[$cid] : '';
                $aout[] = $row;
            }
        }
        $this->assign('list', $aout ? $aout : $list);
        $this->assign('page', $page->showBootstrap());
        $this->assign('search', $search);
        $this->display();
    }

    function changeStatus($id, $status) {
        $m_promotion = new \XWAM\Model\ProductPromotionModel();
        $this->ajaxReturn($m_promotion->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $m_promotion = new \XWAM\Model\ProductPromotionModel();
        $this->ajaxReturn($m_promotion->changeSort($id, $sort));
    }

    function delete($id = '') {
        $m_promotion = new \XWAM\Model\ProductPromotionModel();
        $result = $m_promotion->deletePromotion($id);
        $this->dialogJump($result['status'], $result['info']);
    }

    function promotion_goods_delete($promid = 0, $pid = '') {
        $m_prom = new \XWAM\Model\ProductPromotionModel();
        $spid = $m_prom->where(array('id' => $promid))->getField('product_ids');
        if ($spid && $pid) {
            $apid = explode(',', $pid);
            $spid = ",$spid,";
            foreach ($apid as $tempid) {
                $spid = str_replace(",$tempid,", ',', $spid);
            }
            $spid = trim($spid, ',');
            $apid = explode(',', $spid);
            $m_prom->where(array('id' => $promid))->setField(array('product_ids' => $spid, 'item_count' => count($apid)));
        }
        $this->dialogJump(true);
    }

}
